/***************************************************************************
 *            engine.c
 *
 *  Wed Jun 10 18:08:50 2009
 *  Copyright  2009  linux
 *  <fantasy614@gmail.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
#include <stdio.h>
#include <stdlib.h>

#include "global.h"
#include "engine.h"
#include "think.h"

/*初始化black_side和red_side数组 为now_move,red_move,black_move分配空间*/
bool init_chess()
{
	int i,j;
	int k=0;

	for (i = 3; i <8; i++)
		for (j = 3; j < 12; j++)
		{
			if (chessman_state[i*16+j]>0)
			{
				black_side[k].loc=i*16+j;
				black_side[k].index=chessman_state[i*16+j]-10;
				//g_print("black_side[k].index %d   black_side[k].loc   %d \n",black_side[k].index,black_side[k].loc);
				k++;
			}
		}

	k=0;
	for (i = 8; i <13; i++)
		for (j = 3; j < 12; j++)
		{
			if (chessman_state[i*16+j]>0)
			{
				red_side[k].loc=i*16+j;
				red_side[k].index=chessman_state[i*16+j];
				//g_print("red_side[%d].index %d   red_side[%d].loc   %d \n",k,red_side[k].index,k,red_side[k].loc);

				k++;
			}
		}

	now_move=(struct _move*)malloc(sizeof(struct _move));
	now_move->src_pos=(struct board*)malloc(sizeof(struct board));
	now_move->des_pos=(struct board*)malloc(sizeof(struct board));
	red_move=(struct move*)malloc(sizeof(struct move));
	black_move=(struct move*)malloc(sizeof(struct move));
	chess_state.side_go=RED ;
	return true;
}


/*当一步棋被确定为正确时，执行这个步骤，修改棋盘信息*/
void  change_state(int src,int des)
{
	int i;
	struct piece *src_side,*des_side;
	//判断是哪边
	src_side=(chessman_state[src]>10)?black_side:red_side;
	des_side=(chessman_state[src]>10)?red_side:black_side;
	for (i=0;i<16;i++)
	{
		if (des_side->loc==des)
			des_side->loc=-1;
		else
			des_side++;
	}
	for (i=0;i<16;i++)
	{
		if (src_side->loc==src)
			src_side->loc=des;
		else
			src_side++;
	}
	chessman_state[des]=chessman_state[src];
	chessman_state[src]=0 ;
}


void endchess();


struct move* think( )
{

	struct move*black_move ;
	//int rand_x ;
	//srand((unsigned)time(NULL));


	black_move=(struct move*)malloc(sizeof(struct move*));

	black_move=get_engine_think();


	return black_move ;

};


//判断一个走法是否正确
bool Is_step_right(struct	move  *side_move)
{

	int src=side_move->src ;
	int des=side_move->des ;

	bool flag=false;
	side side_state ;

	side_state=chessman_state[src]/10 ;
	printboard();

	if (chessman_state[src]<0||chessman_state[des]<0)
		return false ;

	if (chessman_state[des])//目标是有棋子
	{
		if (chessman_state[des]/10==side_state)//同一方则返回错误
			return false ;
	}

	switch (chessman_state[src])
	{
		case   R_Horse :
		case   B_Horse:
			flag=check_Horse(src,des);
			break ;

		case R_General:
		case B_General:
			flag=check_General(src,des);
			break;

		case   B_Soldier :
		case   R_Soldier:
			flag=check_Soldier(src,des);
			break ;

		case   R_Cannon :
		case   B_Cannon:
			flag=check_Cannon(src,des);
			break ;

		case R_Advisor:
		case B_Advisor:
			flag=check_Advisor(src,des);
			break;

		case   R_Chariot :
		case   B_Chariot:
			flag=check_Chariot(src,des);
			break;


		case B_Elephant:
		case R_Elephant:
			flag=check_Elephant(src,des);
			break ;

	}
	return flag ;

}
;
//测试用，打印棋盘的信息
void printboard()
{
	int i;
	/*char* chessman_print[18]={"",
	  "将","士","象","马","车","兵","炮","","","",
	  "将","士","象","马","车","兵","炮"  };*/
	for (i=0;i<256;i++)
	{
		if (chessman_state[i]!=-1)
		{
			// printf("%s  ",chessman_print[chessman_state[i]]);
			printf("%2d  ",chessman_state[i]);
		}
		if (i%16==0)
			printf("\n");
	}


}

//判断一个棋局是否结束，还有待修改
bool  is_game_over()
{
	int i;
	for (i=0;i<16;i++)
	{
		switch (black_side[i].index)
		{
			case  4:
				if (check_Horse( black_side[i].loc, red_side[11].loc))
				{
					red_die=true;
					return true;
				}
				break;
			case  5:
				if (check_Chariot( black_side[i].loc, red_side[11].loc))
				{
					red_die=true;
					return true;
				}
				break;
			case  7:
				if (check_Cannon( black_side[i].loc, red_side[11].loc))
				{
					red_die=true;
					return true;
				}
				break;
			case  6:
				if (check_Soldier( black_side[i].loc, red_side[11].loc))
				{
					red_die=true;
					return true;
				}
				break;
		}
	}
	for (i=0;i<16;i++)
	{
		switch (red_side[i].index)
		{
			case  4:
				if (check_Horse( red_side[i].loc, black_side[11].loc))
				{
					black_die=true;
					return true;
				}
				break;
			case  5:
				if (check_Chariot( red_side[i].loc, black_side[11].loc))
				{
					black_die=true;
					return true;
				}
				break;
			case  7:
				if (check_Cannon( red_side[i].loc, black_side[11].loc))
				{
					black_die=true;
					return true;
				}
				break;
			case  6:
				if (check_Soldier( red_side[i].loc, black_side[11].loc))
				{
					black_die=true;
					return true;
				}
				break;
		}
	}
	return false;
}


